/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer.ftypes;

import org.json.JSONObject;
import org.urchr.web.db.DBSession;
import org.webos.java.base.Application;
import org.webos.java.base.Frame;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.Session;
import org.webos.java.base.db.apps.DBApplication;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.base.fs.FileType;
import org.webos.java.explorer.Explorer;
import org.webos.java.base.ApplicationManager;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */
public class ApplicationFileType implements FileType{

    public String getCode() {
        return "application/link";
    }

    public String getName() {
        return "application link";
    }

    public String getDescription() {
        return "Application link";
    }

    public boolean isPlainFile() {
        return false;
    }

    public Frame getEditor(FileDescriptor file, Explorer explorer) {
        return null;
    }

    public String getViewerClass(FileDescriptor aFile) {
        return "explorer.ApplicationLinkViewer";
    }

    public Application startApplication(ApplicationFileDescriptor aDesc, Session aSession){
        try {
            Class cl = ApplicationManager.getApplication(aDesc.getApplicationCode());
            return aSession.startApplication(cl);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return null;
    }

    public JSONObject getViewer(FileDescriptor file) {
        if(!(file instanceof ApplicationFileDescriptor))
            return null;
        ApplicationFileDescriptor appdesc = (ApplicationFileDescriptor) file;
        DBSession dbs = new DBSession();
        try {
            JSONObject data = new JSONObject();
            DBApplication dba = (DBApplication) dbs.createQuery("from DBApplication where code=:code").
                    setString("code", appdesc.getApplicationCode()).uniqueResult();
            if(dba!=null){
                data.put("name", dba.getName());
                data.put("version", dba.getVersion());
                data.put("description", dba.getDescription());
            }
            return data;
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        } finally {
            dbs.close();
        }
        try {
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return new JSONObject();
    }

    public boolean isSystemType() {
        return true;
    }

    public JSONObject messageFromViewer(JSONObject aData, FileDescriptor aFile, Explorer explorer) {
        if(startApplication((ApplicationFileDescriptor) aFile, explorer.getSession())==null){
            return NetworkManager.getErrorObject(10, "Can't start application");
        }
        return null;
    }

    public String getIcon() {
        return "";
    }

    public String[] getExtensions() {
        return null;
    }

}
